<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>

<body>
  <div id="app">
    <div>
      <span>姓名:</span>
      <input type="text" v-model.lazy="uname" />
    </div>
    <div>
      <span>年龄:</span>
      <input type="number" v-model.number="uage" />
    </div>
    <div>
      <span>性别:</span>
      <select v-model="xingbie">
        <option value="男">男</option>
        <option value="女">女</option>
      </select>
    </div>
    <div>
      <button @click="add()">添加/修改</button>
    </div>
    <div>
      <table border="1" cellpadding="10" cellspacing="0">
        <tr>
          <th>序号</th>
          <th>姓名</th>
          <th>年龄</th>
          <th>性别</th>
          <th>操作</th>
        </tr>
        <tr v-for="(item,index) in arr">
          <td>{{index+1}}</td>
          <td>{{item.name}}</td>
          <td>{{item.age}}</td>
          <td>{{item.sex}}</td>
          <td>
            <button @click="del(item.id)">删除</button>
            <button @click="edit(index)">编辑</button>
          </td>
        </tr>
      </table>
    </div>
  </div>
  <script src="./js/vue-2.7.14.js"></script>
  <script>
    const app = new Vue({
      el: '#app',
      data: {
        uname: '',
        uage: 0,
        xingbie: '男',
        arr: [
          { id: 1, name: '陈帅帅', age: 18, sex: '男' },
          { id: 2, name: '丁昌义', age: 28, sex: '女' },
        ],
        bianji: false,
        num: 0
      },
      methods: {

        // 增加
        add() {
          if (this.bianji !== undefined) {
            this.arr[this.num].name = this.uname
            this.arr[this.num].age = this.uage
            this.arr[this.num].sex = this.xingbie
          } else {
            this.arr.push({
              id: this.arr[this.arr.length - 1].id + 1,
              name: this.uname,
              age: this.uage,
              sex: this.xingbie
              // console.log(2) 
            })
          }
          this.uname = ''
          this.uage = 0
          this.xingbie = '男'
        },
        //点击编辑
        edit(index) {
          this.uname = this.arr[index].name
          this.uage = this.arr[index].age
          this.xingbie = this.arr[index].sex
          this.bianji = index
        },
        // 删除
        del(id) {
          this.arr = this.arr.filter(item => {
            return item.id !== id
          })
        }
      }

    })
  </script>
</body>

</html>